/**
 * Copyright 2012 Alfredo "Rainbowbreeze" Morresi
 * 
 * This file is part of Eureka! project.
 * 
 * Eureka! is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * Eureka! is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with Eureka!. If not, see http://www.gnu.org/licenses/.
 */
package it.rainbowbreeze.eureka.ui;

import it.rainbowbreeze.eureka.R;
import it.rainbowbreeze.eureka.common.AppEnv;
import it.rainbowbreeze.eureka.common.LogFacility;
import it.rainbowbreeze.eureka.domain.FilterCategories;
import it.rainbowbreeze.eureka.logic.FiltersManager;
import it.rainbowbreeze.eureka.logic.FiltersManager.FilterStatus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;

/**
 * @author Alfredo "Rainbowbreeze" Morresi
 *
 */
public class FiltersFragment extends Fragment {
    // ------------------------------------------ Private Fields
    private static final String LOG_HASH = FiltersFragment.class.getSimpleName();
    
    private LogFacility mLogFacility;
    private FiltersManager mFiltersManager;
    
    Map<FilterCategories, List<FiltersManager.FilterStatus>> mFilterStatuses;

    // -------------------------------------------- Constructors

    // --------------------------------------- Public Properties

    // -------------------------------------------------- Events
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
        
        AppEnv appEnv = AppEnv.i(getActivity().getApplicationContext());
        mLogFacility = appEnv.getLogFacility();
        mFiltersManager = appEnv.getFiltersManager();
        
        //creates filters array
        mFilterStatuses = new HashMap<FilterCategories, List<FilterStatus>>();
        for (FilterCategories category : FilterCategories.getAll()) {
            mFilterStatuses.put(category,
                    mFiltersManager.loadFiltersStatuses(getActivity(), category));
        }
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mLogFacility.v(LOG_HASH, "onCreateView");
        View view = inflater.inflate(R.layout.frafilters, container, false);

        //load filters
        ViewGroup panel;
        panel = (ViewGroup) view.findViewById(R.id.filters_layEconomicsFilters);
        populateFiltersPanel(inflater, panel, mFilterStatuses.get(FilterCategories.Economics));
        panel = (ViewGroup) view.findViewById(R.id.filters_layEnvironmentFilters);
        populateFiltersPanel(inflater, panel, mFilterStatuses.get(FilterCategories.Environment));
        panel = (ViewGroup) view.findViewById(R.id.filters_laySocialFilters);
        populateFiltersPanel(inflater, panel, mFilterStatuses.get(FilterCategories.Social));
        
        return view;
    }
    
    @Override
    public void onPause() {
        super.onPause();
        mFiltersManager.saveFiltersStatuses(getActivity(), mFilterStatuses);
    }
    
    // ------------------------------------------ Public Methods

    // ----------------------------------------- Private Methods
    private void populateFiltersPanel(
            LayoutInflater inflater,
            ViewGroup container,
            List<FilterStatus> filterStatuses)
    {
        if (null == filterStatuses) return;

        for (int i=0; i<filterStatuses.size(); i++) {
            FiltersManager.FilterStatus status = filterStatuses.get(i);
            View view = inflater.inflate(R.layout.vwfilteritem, null);
            TextView label = (TextView) view.findViewById(R.id.filters_txtFilterItem);
            label.setText(status.filterName);
            ToggleButton button = (ToggleButton) view.findViewById(R.id.filters_btnFilterItem);
            button.setId((int)Math.ceil(Math.random()*Integer.MAX_VALUE));
            button.setChecked(status.isEnabled);
            button.setTag(status);
            button.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    FilterStatus status = (FilterStatus) buttonView.getTag();
                    status.isEnabled = isChecked;
                }
            });
            container.addView(view);
            if (i < (filterStatuses.size()-1)) {
                //add separator
                view = inflater.inflate(R.layout.vwfilterseparator, container);
            }
        }
    }
    
    // ----------------------------------------- Private Classes
}
